#define _CRT_SECURE_NO_WARNINGS	
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
typedef struct ListNode ListNode;
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2)
{
    if (!list1)
        return list2;
    if (!list2)
        return list1;
    ListNode* l1 = list1, * l2 = list2;
    ListNode* newhead, * newtail;
    newhead = newtail = (ListNode*)malloc(sizeof(ListNode));
    while (l1 && l2)
    {
        if (l1->val <= l2->val)
        {
            newtail->next = l1;
            l1 = l1->next;
        }
        else
        {
            newtail->next = l2;
            l2 = l2->next;
        }
        newtail = newtail->next;
    }
    if (l1)
        newtail->next = l1;
    if (l2)
        newtail->next = l2;
    ListNode* ret = newhead->next;
    free(newhead);
    newhead = newtail = NULL;
    return ret;
}